package com.example.admin.yuejian21.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.admin.yuejian21.Adapter.OrderAdapter;
import com.example.admin.yuejian21.Bean.order;
import com.example.admin.yuejian21.R;
import com.example.admin.yuejian21.Service.Service;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.example.admin.yuejian21.activity.LoginActivity.userid;

/**
 * A simple {@link Fragment} subclass.
 */
public class Order1Fragment extends Fragment {
    private RecyclerView recyclerView;

    private OrderAdapter adapter;
    private List<order> list;
    private String SessionID="060pn7sjorpnq5fp53kid5st54";

    public Order1Fragment() {
        // Required empty public constructor
    }
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        recyclerView =
                (RecyclerView) view.findViewById(R.id.recyclerView);
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://172.24.10.175/workout/api.php/").addConverterFactory(GsonConverterFactory.create())
                .build();
        Service service = retrofit.create(Service.class);
        Call<List<order>> call = service.orderList1(userid);
        call.enqueue(new Callback<List<order>>() {
            @Override
            public void onResponse(Call<List<order>> call, Response<List<order>> response) {
                if (getActivity() != null) {
                    list = response.body();
                    ;
                    if(list!=null)
                    {  adapter = new OrderAdapter(getActivity(), list);
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
                        linearLayoutManager.setOrientation(linearLayoutManager.VERTICAL);
                        recyclerView.setLayoutManager(linearLayoutManager);
                        recyclerView.setAdapter(adapter);
                        recyclerView.setItemAnimator(new DefaultItemAnimator());}
                }

            }

            public void onFailure(Call<List<order>> call, Throwable t) {

            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_order1, container, false);
    }

}
